#include "imagehelper.h"

ImageHelper::ImageHelper(QObject *parent) : QObject(parent)
{

}

QIcon ImageHelper::iconFromSvgEmpty(QSize size) {
    QPixmap pixmap(size);

    // 绘制
    QPainter painter(&pixmap);
    // 矩形 边距5
    QRect painter_rect = QRect(QPoint(0,0), size).adjusted(5, 5, -5, -5);
    painter.drawRect(painter_rect);
    // 线(左上右下，左下右上)
    auto lt = painter_rect.topLeft();
    auto lb = painter_rect.bottomLeft();
    auto rt = painter_rect.topRight();
    auto rb = painter_rect.bottomRight();
    painter.drawLine(lt, rb);
    painter.drawLine(lb, rt);

    QIcon icon;
    icon.addPixmap(pixmap);
    return icon;
}

QIcon ImageHelper::iconFromSvgString(QSize &size, QByteArray svgs) {
    if (svgs.isEmpty()) return iconFromSvgEmpty(size);

    // 透明通道图像
    QPixmap pixmap(size);
    pixmap.fill(Qt::transparent);

    // 绘制
    QPainter painter(&pixmap);
    const QRect bounding_rect(QPoint(0, 0), size);

    // 渲染SVG
    QSvgRenderer renderer(svgs);
    QSize defaultSize = renderer.defaultSize();
    defaultSize.scale(size, Qt::KeepAspectRatio);
    QRect defaultRect = QRect(QPoint(0,0), defaultSize);
    defaultRect.translate(QRect(QPoint(0,0), size).center() - defaultRect.center());
    renderer.render(&painter, defaultRect);

    QIcon icon;
    icon.addPixmap(pixmap);
    return icon;
}

QImage ImageHelper::imageFromSvgEmpty(QSize size)
{
    QImage image(size, QImage::Format_ARGB32);

    // 绘制
    QPainter painter(&image);
    // 矩形 边距5
    QRect painter_rect = QRect(QPoint(0,0), size).adjusted(5, 5, -5, -5);
    painter.drawRect(painter_rect);
    // 线(左上右下，左下右上)
    auto lt = painter_rect.topLeft();
    auto lb = painter_rect.bottomLeft();
    auto rt = painter_rect.topRight();
    auto rb = painter_rect.bottomRight();
    painter.drawLine(lt, rb);
    painter.drawLine(lb, rt);

    return image;
}

QImage ImageHelper::imageFromSvgString(QSize size, QByteArray svgs)
{
    if (svgs.isEmpty()) return imageFromSvgEmpty({10,10});

    QImage image(size, QImage::Format_ARGB32);
    image.fill(0x00000000000);

    // 绘制
    QPainter painter(&image);
//    painter.setCompositionMode(QPainter::CompositionMode_Source);
//    painter.fillRect(image.rect(), Qt::transparent);
//    painter.setCompositionMode(QPainter::CompositionMode_SourceOver);

    // 渲染SVG
    QSvgRenderer renderer(svgs);
    QSize defaultSize = renderer.defaultSize();
    defaultSize.scale(size, Qt::KeepAspectRatio);
    QRect defaultRect = QRect(QPoint(0,0), defaultSize);
    defaultRect.translate(QRect(QPoint(0,0), size).center() - defaultRect.center());
    renderer.render(&painter, defaultRect);
    painter.end();

    return image;
}

QPixmap ImageHelper::pixmapFromSvgEmpty(QSize size)
{
    QPixmap pixmap(size);

    // 绘制
    QPainter painter(&pixmap);
    // 矩形 边距5
    QRect painter_rect = QRect(QPoint(0,0), size).adjusted(5, 5, -5, -5);
    painter.drawRect(painter_rect);
    // 线(左上右下，左下右上)
    auto lt = painter_rect.topLeft();
    auto lb = painter_rect.bottomLeft();
    auto rt = painter_rect.topRight();
    auto rb = painter_rect.bottomRight();
    painter.drawLine(lt, rb);
    painter.drawLine(lb, rt);

    return pixmap;
}

QPixmap ImageHelper::pixmapFromSvgString(QSize size, QByteArray svgs) {
    if (svgs.isEmpty()) return pixmapFromSvgEmpty(size);

    QSvgRenderer renderer(svgs);
    QSize defaultSize = renderer.defaultSize();
    defaultSize.scale(size, Qt::KeepAspectRatio);
    QRect defaultRect = QRect(QPoint(0,0), defaultSize);
    defaultRect.translate(QRect(QPoint(0,0), size).center() - defaultRect.center());

    QPixmap pixmap(size);
    pixmap.fill(Qt::transparent);

    // 绘制背景
    QPainter painter(&pixmap);
    painter.setCompositionMode(QPainter::CompositionMode_Source);
    painter.fillRect(pixmap.rect(), Qt::transparent);
    painter.setCompositionMode(QPainter::CompositionMode_SourceOver);

    // 渲染SVG
    renderer.render(&painter, defaultRect);
    painter.end();

    return pixmapFromIcon(size, iconFromSvgString(size, svgs));
}
